<template>
  <el-dialog
      v-model="dialogVisible"
      title="编辑项目信息"
      width="500"
      :before-close="handleClose"
  >
    <el-form :model="projectInfo" label-width="auto" style="max-width: 600px">
      <el-form-item label="项目名">
        <el-input v-model="projectInfo.project_name" />
      </el-form-item>
      <el-form-item label="负责人">
        <el-cascader v-model="projectInfo.charge_person" :options="userOptions" />
      </el-form-item>
      <el-form-item label="项目描述">
        <el-input v-model="projectInfo.project_describe" />
      </el-form-item>
      <el-form-item label="甲方对接人">
        <el-input v-model="projectInfo.first_party" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="submitDia">
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import {checkUserByRole} from "@/api"
import {onMounted, reactive, ref} from "vue"
import {getProInfo, updatePro} from "@/api/Project"
import {ElMessage} from "element-plus"

let currentId

const dialogVisible = ref(false)
let user
let userOptions = ref([])
const projectInfo = reactive({
  project_name:'',
  charge_person:null,
  charge_person_name:null,
  project_describe:'',
  first_party:'',
})

onMounted(() => {
  user = JSON.parse(localStorage.getItem('syhyDocManSys'))
  getUsersList()
})

const getInfo = async () => {
  try {
    const {data:res} = await getProInfo({
      proId:currentId
    })
    if(res.code === 200){
      projectInfo.project_name = res.data.project_name
      projectInfo.charge_person = res.data.charge_person
      projectInfo.charge_person_name = res.data.charge_person_name
      projectInfo.project_describe = res.data.project_describe
      projectInfo.first_party = res.data.first_party
    }
    dialogVisible.value = true
  } catch (e) {
    console.log(e)
  }
}

const getUsersList = async () => {
  try {
    let rolename = []
    let roleUser = []
    const {data:res} = await checkUserByRole()
    const values = Object.values(res.data)
    values.forEach(value => {
      let datas = Object.values(value)
      datas.forEach(data => {
        if(typeof data === "string"){
          rolename.push(data)
        } else {
          roleUser.push(data)
        }
      })
    })
    for (let i = 0; i < 6; i++){
      userOptions.value.push({
        label:rolename[i],
        value:null,
        children:[]
      })
      for (let j = 0; j < roleUser[i].length; j++){
        userOptions.value[i].children.push({
              label:roleUser[i][j].user_name,
              value:roleUser[i][j].user_id,
            }
        )
      }
    }
  } catch (e) {
    console.log(e)
  }
}

const showDia = (id) => {
  currentId = id
  getInfo()
}

const submitDia = async () => {
  const {data:res} = await updatePro({
    proId:currentId,
    newName:projectInfo.project_name,
    newChargePerson:projectInfo.charge_person,
    newDescribe:projectInfo.project_describe,
    newFirst:projectInfo.first_party,
  })
  if(res.code === 200){
    ElMessage({
      message:'操作成功!',
      type:'success'
    })
    handleClose()
  } else {
    ElMessage({
      message:'操作失败!',
      type:'warning'
    })
    handleClose()
  }
}

const handleClose = () => {
  dialogVisible.value = false
  projectInfo.project_name = ''
  projectInfo.charge_person = null
  projectInfo.project_describe = ''
  projectInfo.first_party = ''
}
defineExpose({
  showDia
})
</script>

<style scoped lang="less">

</style>